package org.fastsyncer.connector.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JDBCUtil {

    private final static Logger logger = LoggerFactory.getLogger(JDBCUtil.class);

    public static Connection getConnection(String driver, String url, String username, String password) {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException err) {
            logger.error(err.getClass() + " >> " + err.getMessage() + " >> " + err.getStackTrace());
        } catch (SQLException e) {
            logger.error(e.getClass() + " >> " + e.getMessage() + " >> " + e.getStackTrace());
        }
        return null;
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getClass() + " >> " + e.getMessage() + " >> " + e.getStackTrace());
            }
        }
    }
    
//    public static void main(String[] args) {
//        String url = "jdbc:mysql://10.238.206.222:13306/test?seUnicode=true&characterEncoding=UTF8&useSSL=true";
//        String username = "root";
//        String password = "123";
//        Connection connection = JDBCUtil.getConnection("com.mysql.jdbc.Driver", url, username, password);
//        if(connection!=null){
//            JDBCUtil.close(connection);
//        }else{
//            logger.error("can not connect url.");
//        }
//    }
    
}
